import { _decorator, Color, director, Label, Sprite, UITransform } from 'cc';
import { DebugUtils } from '../Utils/DebugUtils';
import DataManager from '../Runtime/DataManager';
import { UIComponent } from '../Base/UIComponent';
import { BattleJoyStick } from './BattleJoyStick';
import { GChapter, GExp } from '../Common';
import { convertSecondsToTime } from '../Utils';
const { ccclass, property } = _decorator;

@ccclass('BattleUI')
export class BattleUI extends UIComponent {
    @property(Label)
    drawcall: Label

    @property(Label)
    fps: Label

    @property(Label)
    enemyNum: Label

    @property(Label)
    gamespeed: Label

    @property(Label)
    actorLevel: Label

    @property(Label)
    actorExp: Label

    @property(Label)
    actorHP: Label

    @property(Label)
    gameTime: Label

    @property(UITransform)
    expBarBg: UITransform

    @property(UITransform)
    expBar: UITransform

    @property(Sprite)
    dangerEffect: Sprite

    barTotalLength: number

    show(): void {
        DataManager.Ins.jm = this.node.getComponentInChildren(BattleJoyStick)

        this.node.setSiblingIndex(0)

        this.barTotalLength = this.expBarBg.width
    }

    onShowBatch() {
        const du = new DebugUtils()
        du.showDynamicAtlasDebug(true)
        setTimeout(() => {
            du._debugNode.destroy()
        }, 20000);
    }

    onChangeGamespeed() {
        DataManager.Ins.state.gameSpeed += 0.5
        if (DataManager.Ins.state.gameSpeed > 4) {
            DataManager.Ins.state.gameSpeed = 0.5
        }
        this.gamespeed.string = '游戏速度x' + DataManager.Ins.state.gameSpeed
    }

    update() {
        this.drawcall.string = '' + director.root.device.numDrawCalls
        this.fps.string = '' + director.root.fps
        this.enemyNum.string = '' + (DataManager.Ins.state.enemies.length + 1)
        this.actorLevel.string = '' + DataManager.Ins.state.playerLevel
        this.actorHP.string = '' + (DataManager.Ins.state.actors[0]?.hp || 0)

        let totalTime = 1
        for (const i in GChapter[DataManager.Ins.state.levelState.chapter]) {
            totalTime += GChapter[DataManager.Ins.state.levelState.chapter][i].duration
        }
        this.gameTime.string = '' + convertSecondsToTime(totalTime - DataManager.Ins.state.levelState.normalLevelTime)

        this.refreshExpBar()
        this.refreshDangerEffect()
    }
    refreshExpBar() {
        if (GExp[DataManager.Ins.state.playerLevel].exp == -1) {
            this.actorExp.string = 'max'
            this.expBar.width = 1
        } else {
            this.actorExp.string = DataManager.Ins.state.playerExp + '-' + GExp[DataManager.Ins.state.playerLevel].exp
            let curExp = DataManager.Ins.state.playerExp
            let totalExp = GExp[DataManager.Ins.state.playerLevel].exp
            if (DataManager.Ins.state.playerLevel != 1) {
                curExp = DataManager.Ins.state.playerExp - GExp[DataManager.Ins.state.playerLevel - 1].exp
                totalExp = GExp[DataManager.Ins.state.playerLevel].exp - GExp[DataManager.Ins.state.playerLevel - 1].exp
            }
            this.expBar.width = curExp / totalExp * this.barTotalLength
        }

    }
    refreshDangerEffect() {
        const myData = DataManager.Ins.state.actors.find(v => v.id === DataManager.Ins.myId)
        if (!myData) return
        if (myData.hp / myData.attr.hpMax < 0.25) {
            this.dangerEffect.color = new Color(0, 0, 0, 255)
        } else if (myData.hp / myData.attr.hpMax < 0.5) {
            this.dangerEffect.color = new Color(0, 0, 0, 125)
        } else {
            this.dangerEffect.color = new Color(0, 0, 0, 0)
        }
    }
}